<?

$detector = new surf();
//$detector->face_detect($dst);
//$detector->toJpeg();
//print_r( $detector->integralImage($detector->image) );

//////////////

class surf {
	
	protected $image;	// исходное изображение
 
    public function __construct($src = 'img/test2.jpg') {
        if (is_file($src)) $this->image = imagecreatefromjpeg($src);
        else throw new Exception("Couldn't load image");
    }
	
	public function integralImage($img) {
		$w = imagesx($img);
		$h = imagesy($img);
		$res = array();
		$res[0] = array();
		$res[0][0] = imagecolorat($img, 0, 0);
		for ($x = 1; $x < $w; $x++) {
			$res[$x] = array();
			$res[$x][0] = imagecolorat($img, $x, 0) + $res[$x-1][0];
		}		
		for ($y = 1; $y < $h; $y++)
			$res[0][$y] = imagecolorat($img, 0, $y) + $res[0][$y-1];					
		for ($y = 1; $y < $h; $y++)
			for ($x = 1; $x < $w; $x++)
				$res[$x][$y] = imagecolorat($img, $x, $y) + $res[$x-1][$y] + $res[$x][$y-1] - $res[$x-1][$y-1];		
		return $res;
    }
	
	public function integralImageBox($iimg, $x, $y ,$w, $h) {
		$a = 0; $b = 0; $c = 0; $d = 0;
      	if ($x > 0 || $y > 0)
			if ($y <= 0) $d = $iimg[$x-1][$y+$h];
			else if ($x <= 0) $b = $iimg[$x+$w][$y-1];
			else {
				$a = $iimg[$x-1][$y-1];
				$b = $iimg[$x+$w][$y-1];
				$d = $iimg[$x-1][$y+$h];
			}
		$c = $iimg[$w][$h];
		return $a + $c - $b - $d;
    }
	
	
	
	public function toJpeg() {
        $color = imagecolorallocate($this->image, 255, 0, 0); //red
        //imagerectangle($this->image, $this->face['x'], $this->face['y'], $this->face['x']+$this->face['w'], $this->face['y']+ $this->face['w'], $color);
        header('Content-type: image/jpeg');
        imagejpeg($this->image);
    }
}

?>